package com.mijie.homi.service.reports;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mijie.homi.common.domain.SystemMessage;
import com.mijie.homi.common.domain.reports.Crash;
import com.mijie.homi.common.domain.reports.Feedback;
import com.mijie.homi.common.domain.reports.Prosecute;
import com.mijie.homi.common.domain.user.User;
import com.mijie.homi.dao.reports.ReportDao;
import com.mijie.homi.domain.commom.ProsecuteWarning;
import com.mijie.homi.service.message.SystemMessageService;
import com.mijie.homi.service.notify.NotifyUserAgent;
import com.mijie.homi.service.user.UserService;

@Service
public class ReportService {

	@Autowired
	private ReportDao dao;
	@Autowired
	private UserService userService;
	@Autowired
	private SystemMessageService systemMessageService;
	@Autowired
	private NotifyUserAgent notifyUserAgent;
	private Logger logger = Logger.getLogger(ReportService.class);
	
	private final Map<Integer,String> tableMap;
	
	public ReportService(){
		tableMap = new HashMap<Integer,String>();
		tableMap.put(Prosecute.PHOTO, "photo");
		tableMap.put(Prosecute.PHOTO_COMMENT, "photoReview");
		tableMap.put(Prosecute.TOPIC_OPINION, "topicOpinion");
		tableMap.put(Prosecute.TOPIC_OPINION_COMMENT, "topicOpinionReview");
		tableMap.put(Prosecute.USER, "user");
		tableMap.put(Prosecute.MOOD_COMMENT, "moodReview");
		tableMap.put(Prosecute.MOOD_STORY, "mood");
	}
	
	public int addCrash(Crash crash){
		return dao.addCrash(crash);
	}
	
	
	public int addProsecute(Prosecute prosecute){
		String field = "content";
		switch(prosecute.getModule()){
		case Prosecute.PHOTO: field = "smallImage"; break;
		case Prosecute.USER: field = "nickName"; break;
		}
		String content = dao.getProsecuteContentByTargetId(tableMap.get(prosecute.getModule()), field, prosecute.getTargetId());
		prosecute.setContent(content);
		int succ =  dao.addProsecute(prosecute);
		if(succ>0){
			ProsecuteWarning warning = dao.getProsecuteWarning(prosecute.getDefendant());
			if(warning==null){
				warning = new ProsecuteWarning();
				warning.setUserId(prosecute.getDefendant());
				warning.setPlaintiff(","+prosecute.getPlaintiff()+",");
				dao.updateProsecuteWarning(warning);
			}else{
				if(warning.getWarned()==0 && !warning.getPlaintiff().contains(","+prosecute.getPlaintiff()+",")){
					warning.setPlaintiff(warning.getPlaintiff()+prosecute.getPlaintiff()+",");
					dao.updateProsecuteWarning(warning);
					if(warning.getPlaintiffAmount()>=2){
						warning = dao.getProsecuteWarning(prosecute.getDefendant());
						if(warning.getWarned()==0 && warning.getPlaintiffAmount()>=3){
							User user = userService.getUserById(prosecute.getDefendant());
							if(user!=null){
								SystemMessage msg = new SystemMessage();
								msg.setCreateAt(new Date());
								msg.setTitle("被举报温馨提示");
								msg.setContent(content);
								msg.setLink("");
								msg.setLinkName("");
								msg.setContent(user.getNickName()+",您好！请注意您的言行举止，您已被3个人举报过了，如果再被更多的人举报，我们将会屏蔽您的账号和手机，即以后再也无法使用我们的产品，同时也请您采取绿色文明的交友方式，为大家营造一个良好的互助互乐的心情交友氛围，让更多的人倾吐心声，帮助更多的人解决他们的需求，安慰他们，理解他们！希望能得到您的支持，谢谢！");
//								int id = systemMessageService.createSystemMessage(msg);
//								if(id>0){
									notifyUserAgent.sendSystemMessage(prosecute.getDefendant(), msg);
									dao.markProsecuteWarned(prosecute.getDefendant());
//								}
							}//end if(user!=null)
						}//end if(warning.getWarned()==0) 
					}//end if(warning.getPlaintiffAmount()>=2)
				}
			}
		}
		return succ;
	}
	
	public int addFeedback(Feedback feedback){
		if(feedback == null || feedback.getContent() == null)
			return 0;
		if(Pattern.compile("script", Pattern.CASE_INSENSITIVE).matcher(feedback.getContent()).find())
			return 0;
		return dao.addFeedback(feedback);
	}
	
}
